import { Module } from '@nestjs/common';

import { TypeOrmModule } from '@nestjs/typeorm';

import { EntityExistConstraint } from './constraints/entity-exist.constraint';
import { UniqueConstraint } from './constraints/unique.constraint';

const providers = [EntityExistConstraint, UniqueConstraint];

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory: () => ({
        type: 'mysql',
        host: process.env.DB_HOST,
        port: parseInt(process.env.DB_PORT, 10),
        username: process.env.DB_USERNAME,
        password: process.env.DB_PASSWORD,
        database: process.env.DB_DATABASE,
        entities: [__dirname + './../../**/*.entity{.ts,.js}'],
        synchronize: process.env.DB_SYNCHRONIZE, // 开发环境可开启，生产建议关闭
      }),
    }),
  ],
  providers,
  exports: providers,
})
export class DatabaseModule {}
